package com.codingfutrue.glacieruser.controller;

import com.codingfutrue.glacieruser.dto.LoginDTO;
import com.codingfutrue.glacieruser.entity.Emp;
import com.codingfutrue.glacieruser.service.EmpService;
import com.codingfuture.glaciercommon.util.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Author: Ji
 * @DateTime: 2025-04-14 17:28
 * @Description:
 **/
@RestController
@Slf4j
@Api("登录界面")
@RefreshScope
public class LoginController {

    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginDTO loginDTO, HttpSession session) {
        Emp emp = empService.login(loginDTO);

        if (emp != null) {
            session.setAttribute("emp", emp);
            return Result.login(emp.getLoginName());
        }
        return Result.err();
    }

    @PostMapping("/register")
    public Result<Void> register(@RequestBody LoginDTO registerDTO) {
        return empService.register(registerDTO) ?
                Result.register() : Result.err();
    }
}
